/*************************************************************************** 
 * (c) 2006 Dmitry Osinovsky, osdmsoftware@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace OsdmSoftware.CommonLibrary
{
    public partial class Props: Form
    {
        IList list;
        Type[] types;
        PropertyEditors pe;
        public Props(IList list, Type[] types, PropertyEditors pe)
        {
            InitializeComponent();
            this.list = list;
            this.types = types;
            this.pe = pe;
            if (types.Length > 1)
            {
                foreach (Type t in types)
                {
                    MenuItem mi = new MenuItem();
                    mi.Text = t.Name;
                    mi.Click += new System.EventHandler(this.menuItem1_Click);
                    this.menuItem1.MenuItems.Add(mi);
                }
            }
            else
            {
                this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            }

            FillListBox();
        }

        void FillListBox()
        {
            int i = listBox1.SelectedIndex;
            listBox1.BeginUpdate();
            listBox1.Items.Clear();
            foreach (object obj in list)
            {
                /*string s = obj.ToString();
                if (s == null)
                    listBox1.Items.Add("(empty)");
                else
                    listBox1.Items.Add(obj.ToString());*/
                listBox1.Items.Add(obj);
            }
            listBox1.EndUpdate();
            if (i < listBox1.Items.Count)
                listBox1.SelectedIndex = i;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            MenuItem mi = ((MenuItem)sender);
            int i = mi.Parent.MenuItems.IndexOf(mi);
            ConstructorInfo c = types[i].GetConstructor(new Type[0]);
            object o = c.Invoke(null);
            if (pe.EditObject(o))
            {
                list.Add(o);
                FillListBox();
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;

            object o = listBox1.SelectedItem; //list[listBox1.SelectedIndex];
            if (pe.EditObject(o))
            {
                FillListBox();
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;

            if (MessageBox.Show("Really delete?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                list.RemoveAt(listBox1.SelectedIndex);
                FillListBox();
            }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;

            if (listBox1.SelectedIndex == 0)
                return;

            object o = listBox1.SelectedItem;
            list.RemoveAt(listBox1.SelectedIndex);
            list.Insert(listBox1.SelectedIndex - 1, o);
            listBox1.SelectedIndex = listBox1.SelectedIndex - 1;
            FillListBox();
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;

            if (listBox1.SelectedIndex == listBox1.Items.Count - 1)
                return;

            object o = listBox1.SelectedItem;
            list.RemoveAt(listBox1.SelectedIndex);
            list.Insert(listBox1.SelectedIndex + 1, o);
            listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
            FillListBox();
        }
    }
}